package com.meixiang.main;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;

import com.meixiang.R;
import com.meixiang.global.AbActivityManager;
import com.meixiang.http.HttpUtils;
import com.meixiang.tool.StatusBarUtil;
import com.meixiang.view.SwipeBackLayout;
import com.meixiang.view.TitleView;
import com.meixiang.view.statusView.ActivityStatusView;

/**
 * 作者：黑哥 on 2016/3/29 09:53
 */
public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {
    protected String TAG = this.getClass().getSimpleName();
    protected Context context;
    protected Activity activity;
    //滑动关闭activity
    private SwipeBackLayout layout;
    //对外提供一个title的方法
    protected TitleView titles;

    private boolean isFirst = true;
    protected TitleView titleView;
    //无数据无网络加载中布局
    public ActivityStatusView status;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        activity=this;
        context = this;
        AbActivityManager.getInstance().addActivity(this);
        titleView = (TitleView) this.findViewById(R.id.title);
        initSwipeBack();
        HttpUtils.init(this);
        //创建状态布局
        status=new ActivityStatusView(context,activity);
        status.setTitleView(titleView);
        initTitle();
        initView();
        setData();
        StatusBarUtil.StatusBarLightMode(this);

    }

    protected abstract void initTitle();

    protected abstract void initView();

    protected abstract void setData();



    protected void setTitleBackground(String title, int leftImageId) {
        TitleView titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTColor(ContextCompat.getColor(context, R.color.white));
        titleView.setBackgroundColor(ContextCompat.getColor(context, R.color.staging_theme_color));
        titleView.setTitle(title);
        titleView.setLeftImageButton(leftImageId);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void setRightTitleBackground(String title, String rightMessage, int leftImageId, View.OnClickListener clickListener) {
        TitleView titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTColor(ContextCompat.getColor(context, R.color.white));
        titleView.setBackgroundColor(ContextCompat.getColor(context, R.color.staging_theme_color));
        titleView.setRightTextButton(rightMessage);
        titleView.setRightTextCorlr(ContextCompat.getColor(context, R.color.white));
        titleView.showRightButton(clickListener);
        titleView.setTitle(title);
        titleView.setLeftImageButton(leftImageId);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * Created by yinsujun on 2016/4/1 0001 13:33
     * 设置标题及返回键
     * title:标题名称
     * id：title
     */
    protected void setTitle(String title) {
        TitleView titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTitle(title);
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void setTitle(String title, int rightRid, View.OnClickListener onClickListener) {
        titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTitle(title);
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleView.setRightImageButton(rightRid);
        titleView.showRightButton(onClickListener);
    }

    /**
     * Created by yinsujun on 2016/4/1 0001 13:33
     * 设置标题 自定义按钮监听
     * title:标题名称
     * id：title
     */
    protected void setTitle(String title, View.OnClickListener clickListener) {
        titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTitle(title);
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(clickListener);
    }

    /**
     * Created by yinsujun on 2016/4/1 0001 13:33
     * 设置标题,左侧描述及返回键
     * title:标题名称
     * id：title
     */
    protected void setTitle(String title, String leftMessage) {
        TitleView titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTitle(title);
        titleView.setLeftTextButton(leftMessage);
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void setTitle(String title, String leftMessage, String rightMessage, int rightImageId, View.OnClickListener clickListener) {
        titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTitle(title);
        titleView.setLeftTextButton(leftMessage);
        titleView.setRightTextButton(rightMessage);
        if (rightImageId != 0) {
            titleView.setRightImageButton(rightImageId);
        }
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(clickListener);
        titleView.showRightButton(clickListener);
    }

    protected void setRightTitle(String title, String rightMessage, View.OnClickListener rightOnClick) {
        TitleView titleView = (TitleView) this.findViewById(R.id.title);
        titleView.setTitle(title);
        titleView.setRightTextButton(rightMessage);
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleView.showRightButton(rightOnClick);
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(context, cls);
        startActivity(intent);
    }

    public void startActivity(Bundle bundle, Class<?> cls) {
        Intent intent = new Intent(context, cls);

        intent.putExtras(bundle);
        startActivity(intent);
    }
    public void startActivityForResult(Bundle bundle,int requestCode ,Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.putExtras(bundle);
        startActivityForResult(intent,requestCode);
    }

    protected void initSwipeBack() {
        if ((getClass() == MainActivity.class)) {

        } else {
            layout = (SwipeBackLayout) LayoutInflater.from(this).inflate(
                    R.layout.swipeback, null);
            layout.attachToActivity(this);
        }

    }


    /**
     * 设置activity启动动画
     */
    @Override
    protected void onStart() {
        super.onStart();
        if (isFinishing() || isFirst && getClass() != MainActivity.class) {
            overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_left_out);
            isFirst = false;
        }
    }

    /**
     * 设置activity关闭动画
     */
    @Override
    public void finish() {
        super.finish();
        if (getClass() != MainActivity.class)
            overridePendingTransition(R.anim.base_slide_left_in, R.anim.base_slide_right_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HttpUtils.tagCancelRequest(this);
    }
    /**
     *
     * @param message  提示信息
     * @param btnNegative  取消按钮
     * @param btnPositive  确认按钮
     * @param positiveListener 确认按钮点击事件
     * @return
     */
    protected AlertDialog.Builder showAlertDialog(String message, String btnNegative, String btnPositive, Dialog.OnClickListener positiveListener){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle("温馨提示");
        builder.setMessage(message);
        builder.setNegativeButton(btnNegative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton(btnPositive,positiveListener);
        builder.show();
        return  builder;
    }
}
